<!DOCTYPE html>

<html lang="en">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>使用视图（Using Views） &mdash; Phalcon 2.0.0 文档</title>
    <meta name="keywords" content="php, phalcon, phalcon php, php framework, faster php framework"><link rel="stylesheet" type="text/css" href="../_static/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="http://static.phalconphp.com/css/phalcon.min.css" />
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400' rel='stylesheet' type='text/css'>
    <link href="http://fonts.googleapis.com/css?family=Merriweather:400,700" rel="stylesheet" type="text/css" />
    <!--
    EUROPE <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=latin-ext' rel='stylesheet' type='text/css'>
    GREEK <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=greek-ext' rel='stylesheet' type='text/css'>
    RUSSIA <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=cyrillic-ext,latin' rel='stylesheet' type='text/css'>
    -->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.0.0',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script src="../_static/jquery.min.js"></script>
    <script type="text/javascript" src="../_static/docs.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="top" title="Phalcon 2.0.0 文档" href="../index.html" />
    <link rel="next" title="视图助手（View Helpers）" href="tags.html" />
    <link rel="prev" title="对象文档映射 ODM (Object-Document Mapper)" href="odm.html" /> 
  </head>
  <body>

<header class="page-header">
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-container">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand phalcon-logo" href="/"><span itemprop="name" class="sr-only">Phalcon PHP</span></a>
            </div>

            <div class="collapse navbar-collapse navbar-right" id="main-menu-container">
                <ul class="nav navbar-nav main-menu">
                  <li class="first"><a href="http://phalconphp.com/en/download" class="header-nav-link">Download</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="http://blog.phalconphp.com/" class="header-nav-link" target="_blank">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                </ul>
            </div>
        </div>
    </nav>
  </header>

<div class="heading">
    <div class="container">
        <div class="row">
            <h2>Documentation</h2>
        </div>
    </div>
</div>
    <!--<div class="header-line">
      <div class="size-wrap">
        <div class="header-line-title title-white">Documentation</div>
      </div>
    </div>-->
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="tags.html" title="视图助手（View Helpers）"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="odm.html" title="对象文档映射 ODM (Object-Document Mapper)"
             accesskey="P">上一页</a> |</li>
        <li><a href="http://phalconphp.com">Home</a> &raquo;</li>
        <li><a href="../index.html">Phalcon 2.0.0 文档</a> &raquo;</li> 
      </ul>
    </div>  

      <table width="100%" align="center" cellpadding="0" cellspacing="0">
        <tr>
      <td class="primary-box" width="25%" valign="top">
            <div>
            <div id="searchbox" style="">
                <!--<form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="search" name="q" size="25" placeholder="Search">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>-->
                <div style="width:200px;padding:10px">
                  <gcse:searchbox-only></gcse:searchbox-only>
                </div>
            </div>
            </div>
            <div style="padding:5px;padding-left:10px">
              <div id="carbonads-container">
                <div class="carbonad"><div id="azcarbon"></div>
                <script type="text/javascript">var z = document.createElement("script"); z.type = "text/javascript"; z.async = true; z.src = "http://engine.carbonads.com/z/56496/azcarbon_2_1_0_VERT"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s);</script>
                </div></div>
            </div>
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference internal" href="#">使用视图（Using Views）</a><ul>
<li><a class="reference internal" href="#integrating-views-with-controllers">集成视图到控制器（Integrating Views with Controllers）</a></li>
<li><a class="reference internal" href="#hierarchical-rendering">分层渲染（Hierarchical Rendering）</a><ul>
<li><a class="reference internal" href="#using-templates">使用模版（Using Templates）</a></li>
<li><a class="reference internal" href="#control-rendering-levels">渲染级别控制（Control Rendering Levels）</a></li>
<li><a class="reference internal" href="#disabling-render-levels">关闭渲染级别（Disabling render levels）</a></li>
<li><a class="reference internal" href="#picking-views">选择视图（Picking Views）</a></li>
<li><a class="reference internal" href="#disabling-the-view">关闭视图（Disabling the view）</a></li>
</ul>
</li>
<li><a class="reference internal" href="#simple-rendering">简单渲染（Simple Rendering）</a></li>
<li><a class="reference internal" href="#using-partials">使用局部模版（Using Partials）</a></li>
<li><a class="reference internal" href="#transfer-values-from-the-controller-to-views">控制器传值给视图（Transfer values from the controller to views）</a></li>
<li><a class="reference internal" href="#using-models-in-the-view-layer">在视图中使用模型（Using models in the view layer）</a></li>
<li><a class="reference internal" href="#caching-view-fragments">缓存视图片段（Caching View Fragments）</a></li>
<li><a class="reference internal" href="#template-engines">模版引擎（Template Engines）</a><ul>
<li><a class="reference internal" href="#creating-your-own-template-engine-adapter">创建模版引擎（Creating your own Template Engine Adapter）</a></li>
<li><a class="reference internal" href="#changing-the-template-engine">替换模版引擎（Changing the Template Engine）</a></li>
</ul>
</li>
<li><a class="reference internal" href="#injecting-services-in-view">注入服务到视图（Injecting services in View）</a></li>
<li><a class="reference internal" href="#stand-alone-component">独立的组件（Stand-Alone Component）</a><ul>
<li><a class="reference internal" href="#id3">分层渲染（Hierarchical Rendering）</a></li>
<li><a class="reference internal" href="#id4">简单渲染（Simple Rendering）</a></li>
</ul>
</li>
<li><a class="reference internal" href="#view-events">视图事件（View Events）</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="odm.html" title="上一章">&lt; 对象文档映射 ODM (Object-Document Mapper)</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="tags.html" title="下一章">视图助手（View Helpers） &gt;</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/reference/views.txt" rel="nofollow">显示源代码</a></li>
            </ul>
        </td>
          <td class="second-box" valign="top">
            <div class="document">
                <div class="documentwrapper">
                  <div class="bodywrapper">
                    <div class="body" >
                      
  <div class="section" id="using-views">
<h1>使用视图（Using Views）<a class="headerlink" href="#using-views" title="永久链接至标题">¶</a></h1>
<p>视图代表了应用程序中的用户界面. 视图通常是在 HTML 文件里嵌入 PHP 代码，这些代码仅仅是用来展示数据。
视图的任务是当应用程序发生请求时，提供数据给 web 浏览器或者其他工具。</p>
<p><a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 和 <a class="reference internal" href="../api/Phalcon_Mvc_View_Simple.html"><em>Phalcon\Mvc\View\Simple</em></a>
负责管理你的MVC应用程序的视图(View)层。</p>
<div class="section" id="integrating-views-with-controllers">
<h2>集成视图到控制器（Integrating Views with Controllers）<a class="headerlink" href="#integrating-views-with-controllers" title="永久链接至标题">¶</a></h2>
<p>当某个控制器已经完成了它的周期，Phalcon自动将执行传递到视图组件。视图组件将在视图文件夹中寻找一个文件夹名与最后一个控制器名相同,文件命名与最后一个动作相同的文件执行。例如，如果请求的URL <em>http://127.0.0.1/blog/posts/show/301</em>, Phalcon将如下所示的方式按解析URL:</p>
<table border="1" class="docutils">
<colgroup>
<col width="63%" />
<col width="37%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Server Address</td>
<td>127.0.0.1</td>
</tr>
<tr class="row-even"><td>Phalcon Directory</td>
<td>blog</td>
</tr>
<tr class="row-odd"><td>Controller</td>
<td>posts</td>
</tr>
<tr class="row-even"><td>Action</td>
<td>show</td>
</tr>
<tr class="row-odd"><td>Parameter</td>
<td>301</td>
</tr>
</tbody>
</table>
<p>调度程序将寻找一个“PostsController”控制器及其“showAction”动作。对于这个示例的一个简单的控制器文件：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
    <span class="p">{</span>

    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">showAction</span><span class="p">(</span><span class="nv">$postId</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// Pass the $postId parameter to the view</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">setVar</span><span class="p">(</span><span class="s2">&quot;postId&quot;</span><span class="p">,</span> <span class="nv">$postId</span><span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>setVar允许我们创建视图变量，这样可以在视图模板中使用它们。上面的示例演示了如何传递 $postId 参数到相应的视图模板。</p>
</div>
<div class="section" id="hierarchical-rendering">
<h2>分层渲染（Hierarchical Rendering）<a class="headerlink" href="#hierarchical-rendering" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 支持文件的层次结构，在Phalcon中是默认的视图渲染组件。这个层次结构允许通用的布局点(常用的视图)和以控制器命名的文件夹中定义各自的视图模板</p>
<p>该组件使用默认PHP本身作为模板引擎，因此视图应该以.phtml作为拓展名。如果视图目录是 <em>app/views</em> ，视图组件会自动找到这三个视图文件。</p>
<p>你不需要实现上面提到的所有文件。在文件的层次结构中 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 将简单地移动到下一个视图级别。如果这三个视图文件被实现，他们将被按下面方式处理:</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/posts/show.phtml --&gt;</span>

<span class="nt">&lt;h3&gt;</span>This is show view!<span class="nt">&lt;/h3&gt;</span>

<span class="nt">&lt;p&gt;</span>I have received the parameter <span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$postId</span> <span class="cp">?&gt;</span><span class="nt">&lt;/p&gt;</span>
</pre></div>
</div>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/layouts/posts.phtml --&gt;</span>

<span class="nt">&lt;h2&gt;</span>This is the &quot;posts&quot; controller layout!<span class="nt">&lt;/h2&gt;</span>

<span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">()</span> <span class="cp">?&gt;</span>
</pre></div>
</div>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/index.phtml --&gt;</span>
<span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;head&gt;</span>
        <span class="nt">&lt;title&gt;</span>Example<span class="nt">&lt;/title&gt;</span>
    <span class="nt">&lt;/head&gt;</span>
    <span class="nt">&lt;body&gt;</span>

        <span class="nt">&lt;h1&gt;</span>This is main layout!<span class="nt">&lt;/h1&gt;</span>

        <span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">()</span> <span class="cp">?&gt;</span>

    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<p>注意方法 <em>$this-&gt;getContent()</em> 被调用的这行。这种方法指示 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 在这里注入前面视图层次结构执行的内容。在上面的示例中，输出将会是：</p>
<div class="figure align-center">
<img alt="../_images/views-1.png" src="../_images/views-1.png" />
</div>
<p>请求生成的HTML的将为：</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/index.phtml --&gt;</span>
<span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;head&gt;</span>
        <span class="nt">&lt;title&gt;</span>Example<span class="nt">&lt;/title&gt;</span>
    <span class="nt">&lt;/head&gt;</span>
    <span class="nt">&lt;body&gt;</span>

        <span class="nt">&lt;h1&gt;</span>This is main layout!<span class="nt">&lt;/h1&gt;</span>

        <span class="c">&lt;!-- app/views/layouts/posts.phtml --&gt;</span>

        <span class="nt">&lt;h2&gt;</span>This is the &quot;posts&quot; controller layout!<span class="nt">&lt;/h2&gt;</span>

        <span class="c">&lt;!-- app/views/posts/show.phtml --&gt;</span>

        <span class="nt">&lt;h3&gt;</span>This is show view!<span class="nt">&lt;/h3&gt;</span>

        <span class="nt">&lt;p&gt;</span>I have received the parameter 101<span class="nt">&lt;/p&gt;</span>

    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<div class="section" id="using-templates">
<h3>使用模版（Using Templates）<a class="headerlink" href="#using-templates" title="永久链接至标题">¶</a></h3>
<p>模板视图可以用来分享共同的视图代码。他们作为控制器的布局，所以你需要放在布局目录。</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">initialize</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">setTemplateAfter</span><span class="p">(</span><span class="s1">&#39;common&#39;</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">lastAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">flash</span><span class="o">-&gt;</span><span class="na">notice</span><span class="p">(</span><span class="s2">&quot;These are the latest posts&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/index.phtml --&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;head&gt;</span>
        <span class="nt">&lt;title&gt;</span>Blog&#39;s title<span class="nt">&lt;/title&gt;</span>
    <span class="nt">&lt;/head&gt;</span>
    <span class="nt">&lt;body&gt;</span>
        <span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">()</span> <span class="cp">?&gt;</span>
    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/layouts/common.phtml --&gt;</span>

<span class="nt">&lt;ul</span> <span class="na">class=</span><span class="s">&quot;menu&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/&quot;</span><span class="nt">&gt;</span>Home<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
    <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/articles&quot;</span><span class="nt">&gt;</span>Articles<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
    <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/contact&quot;</span><span class="nt">&gt;</span>Contact us<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
<span class="nt">&lt;/ul&gt;</span>

<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">()</span> <span class="cp">?&gt;</span><span class="nt">&lt;/div&gt;</span>
</pre></div>
</div>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/layouts/posts.phtml --&gt;</span>

<span class="nt">&lt;h1&gt;</span>Blog Title<span class="nt">&lt;/h1&gt;</span>

<span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">()</span> <span class="cp">?&gt;</span>
</pre></div>
</div>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/posts/last.phtml --&gt;</span>

<span class="nt">&lt;article&gt;</span>
    <span class="nt">&lt;h2&gt;</span>This is a title<span class="nt">&lt;/h2&gt;</span>
    <span class="nt">&lt;p&gt;</span>This is the post content<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/article&gt;</span>

<span class="nt">&lt;article&gt;</span>
    <span class="nt">&lt;h2&gt;</span>This is another title<span class="nt">&lt;/h2&gt;</span>
    <span class="nt">&lt;p&gt;</span>This is another post content<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/article&gt;</span>
</pre></div>
</div>
<p>最终的输出如下:</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="c">&lt;!-- app/views/index.phtml --&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
    <span class="nt">&lt;head&gt;</span>
        <span class="nt">&lt;title&gt;</span>Blog&#39;s title<span class="nt">&lt;/title&gt;</span>
    <span class="nt">&lt;/head&gt;</span>
    <span class="nt">&lt;body&gt;</span>

        <span class="c">&lt;!-- app/views/layouts/common.phtml --&gt;</span>

        <span class="nt">&lt;ul</span> <span class="na">class=</span><span class="s">&quot;menu&quot;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/&quot;</span><span class="nt">&gt;</span>Home<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/articles&quot;</span><span class="nt">&gt;</span>Articles<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
            <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/contact&quot;</span><span class="nt">&gt;</span>Contact us<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
        <span class="nt">&lt;/ul&gt;</span>

        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span>

            <span class="c">&lt;!-- app/views/layouts/posts.phtml --&gt;</span>

            <span class="nt">&lt;h1&gt;</span>Blog Title<span class="nt">&lt;/h1&gt;</span>

            <span class="c">&lt;!-- app/views/posts/last.phtml --&gt;</span>

            <span class="nt">&lt;article&gt;</span>
                <span class="nt">&lt;h2&gt;</span>This is a title<span class="nt">&lt;/h2&gt;</span>
                <span class="nt">&lt;p&gt;</span>This is the post content<span class="nt">&lt;/p&gt;</span>
            <span class="nt">&lt;/article&gt;</span>

            <span class="nt">&lt;article&gt;</span>
                <span class="nt">&lt;h2&gt;</span>This is another title<span class="nt">&lt;/h2&gt;</span>
                <span class="nt">&lt;p&gt;</span>This is another post content<span class="nt">&lt;/p&gt;</span>
            <span class="nt">&lt;/article&gt;</span>

        <span class="nt">&lt;/div&gt;</span>

    <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="control-rendering-levels">
<h3>渲染级别控制（Control Rendering Levels）<a class="headerlink" href="#control-rendering-levels" title="永久链接至标题">¶</a></h3>
<p>如上所述，<a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 支持视图分层。你可能需要控制视图组件的渲染级别。方法 PhalconMvc\View::setRenderLevel() 提供这个功能。</p>
<p>这种方法可以从控制器调用或是从上级视图层干涉渲染过程。</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
    <span class="p">{</span>

    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">findAction</span><span class="p">()</span>
    <span class="p">{</span>

        <span class="c1">// This is an Ajax response so it doesn&#39;t generate any kind of view</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">setRenderLevel</span><span class="p">(</span><span class="nx">View</span><span class="o">::</span><span class="na">LEVEL_NO_RENDER</span><span class="p">);</span>

        <span class="c1">//...</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">showAction</span><span class="p">(</span><span class="nv">$postId</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// Shows only the view related to the action</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">setRenderLevel</span><span class="p">(</span><span class="nx">View</span><span class="o">::</span><span class="na">LEVEL_ACTION_VIEW</span><span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>可用的渲染级别:</p>
<table border="1" class="docutils">
<colgroup>
<col width="22%" />
<col width="71%" />
<col width="7%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">类常量</th>
<th class="head">解释</th>
<th class="head">顺 序</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>LEVEL_NO_RENDER</td>
<td>表明要避免产生任何形式的显示。</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td>LEVEL_ACTION_VIEW</td>
<td>生成显示到视图关联的动作。</td>
<td>1</td>
</tr>
<tr class="row-even"><td>LEVEL_BEFORE_TEMPLATE</td>
<td>生成显示到控制器模板布局之前。</td>
<td>2</td>
</tr>
<tr class="row-odd"><td>LEVEL_LAYOUT</td>
<td>生成显示到控制器布局。</td>
<td>3</td>
</tr>
<tr class="row-even"><td>LEVEL_AFTER_TEMPLATE</td>
<td>生成显示到控制器模板布局后。</td>
<td>4</td>
</tr>
<tr class="row-odd"><td>LEVEL_MAIN_LAYOUT</td>
<td>生成显示到主布局。文件： views/index.phtml</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="disabling-render-levels">
<h3>关闭渲染级别（Disabling render levels）<a class="headerlink" href="#disabling-render-levels" title="永久链接至标题">¶</a></h3>
<p>你可以永久或暂时禁用渲染级别。如果不在整个应用程序使用，可以永久禁用一个级别：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>

<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">(){</span>

    <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>

    <span class="c1">//Disable several levels</span>
    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">disableLevel</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
        <span class="nx">View</span><span class="o">::</span><span class="na">LEVEL_LAYOUT</span>      <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
        <span class="nx">View</span><span class="o">::</span><span class="na">LEVEL_MAIN_LAYOUT</span> <span class="o">=&gt;</span> <span class="k">true</span>
    <span class="p">));</span>

    <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>

<span class="p">},</span> <span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
<p>或者在某些应用程序的一部分暂时或禁用:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
    <span class="p">{</span>

    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">findAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">disableLevel</span><span class="p">(</span><span class="nx">View</span><span class="o">::</span><span class="na">LEVEL_MAIN_LAYOUT</span><span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="picking-views">
<h3>选择视图（Picking Views）<a class="headerlink" href="#picking-views" title="永久链接至标题">¶</a></h3>
<p>如上所述, 当 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 由 <a class="reference internal" href="../api/Phalcon_Mvc_Application.html"><em>Phalcon\Mvc\Application</em></a> 视图渲染的是最后的一个相关的控制器和执行动作。你可以使用 Phalcon\Mvc\View::pick() 方法覆盖它。</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">ProductsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">listAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// Pick &quot;views-dir/products/search&quot; as view to render</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">pick</span><span class="p">(</span><span class="s2">&quot;products/search&quot;</span><span class="p">);</span>

        <span class="c1">// Pick &quot;views-dir/books/list&quot; as view to render</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">pick</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;books&#39;</span><span class="p">));</span>

        <span class="c1">// Pick &quot;views-dir/products/search&quot; as view to render</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">pick</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="mi">1</span> <span class="o">=&gt;</span> <span class="s1">&#39;search&#39;</span><span class="p">));</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="disabling-the-view">
<h3>关闭视图（Disabling the view）<a class="headerlink" href="#disabling-the-view" title="永久链接至标题">¶</a></h3>
<p>如果你的控制器不在视图里产生(或没有)任何输出，你可以禁用视图组件来避免不必要的处理：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">UsersController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">closeSessionAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//Close session</span>
        <span class="c1">//...</span>

        <span class="c1">//An HTTP Redirect</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span><span class="o">-&gt;</span><span class="na">redirect</span><span class="p">(</span><span class="s1">&#39;index/index&#39;</span><span class="p">);</span>

        <span class="c1">//Disable the view to avoid rendering</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">disable</span><span class="p">();</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>你可以返回一个“response”的对象，避免手动禁用视图:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">UsersController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">closeSessionAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//Close session</span>
        <span class="c1">//...</span>

        <span class="c1">//An HTTP Redirect</span>
        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">response</span><span class="o">-&gt;</span><span class="na">redirect</span><span class="p">(</span><span class="s1">&#39;index/index&#39;</span><span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="simple-rendering">
<h2>简单渲染（Simple Rendering）<a class="headerlink" href="#simple-rendering" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="../api/Phalcon_Mvc_View_Simple.html"><em>Phalcon\Mvc\View\Simple</em></a> 是 <tt class="xref doc docutils literal"><span class="pre">Phalcon\Mvc\View</span></tt> 的大多数的设计思想，但缺少文件的层次结构是它们的主要区别。</p>
<p>该组件允许开发人员控制渲染视图时，视图所在位置。
此外，该组件可以利用从视图中继承的可用的模板引擎。比如 <a class="reference internal" href="volt.html"><em>Volt</em></a> 和其他的一些模板引擎。</p>
<p>默认使用该的组件必须替换服务容器：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View\Simple</span> <span class="k">as</span> <span class="nx">SimpleView</span><span class="p">;</span>

<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>

    <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SimpleView</span><span class="p">();</span>

    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s1">&#39;../app/views/&#39;</span><span class="p">);</span>

    <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>

<span class="p">},</span> <span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
<p>自动渲染必须在 :doc:<a href="#id1"><span class="problematic" id="id2">`</span></a>Phalcon\Mvc\Application &lt;applications&gt;`被禁用 (如果需要):</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Application</span><span class="p">;</span>

<span class="k">try</span> <span class="p">{</span>

    <span class="nv">$application</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Application</span><span class="p">(</span><span class="nv">$di</span><span class="p">);</span>

    <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">useImplicitView</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>

    <span class="k">echo</span> <span class="nv">$application</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>

<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>渲染一个视图必须显式地调用render方法来指定你想显示的视图的相对路径：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">\Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//Render &#39;views-dir/index.phtml&#39;</span>
        <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;index&#39;</span><span class="p">);</span>

        <span class="c1">//Render &#39;views-dir/posts/show.phtml&#39;</span>
        <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;posts/show&#39;</span><span class="p">);</span>

        <span class="c1">//Render &#39;views-dir/index.phtml&#39; passing variables</span>
        <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;index&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;posts&#39;</span> <span class="o">=&gt;</span> <span class="nx">Posts</span><span class="o">::</span><span class="na">find</span><span class="p">()));</span>

        <span class="c1">//Render &#39;views-dir/posts/show.phtml&#39; passing variables</span>
        <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;posts/show&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;posts&#39;</span> <span class="o">=&gt;</span> <span class="nx">Posts</span><span class="o">::</span><span class="na">find</span><span class="p">()));</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="using-partials">
<h2>使用局部模版（Using Partials）<a class="headerlink" href="#using-partials" title="永久链接至标题">¶</a></h2>
<p>局部模板是把渲染过程分解成更简单、更好管理的、可以重用不同部分的应用程序块的另一种方式。你可以移动渲染特定响应的代码块到自己的文件。</p>
<p>使用局部模板的一种方法是把它们作为相等的子例程：作为一种移动细节视图，这样您的代码可以更容易地被理解。例如，您可能有一个视图看起来像这样：</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;top&quot;</span><span class="nt">&gt;</span><span class="cp">&lt;?php</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">partial</span><span class="p">(</span><span class="s2">&quot;shared/ad_banner&quot;</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="nt">&lt;/div&gt;</span>

<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;h1&gt;</span>Robots<span class="nt">&lt;/h1&gt;</span>

    <span class="nt">&lt;p&gt;</span>Check out our specials for robots:<span class="nt">&lt;/p&gt;</span>
    ...
<span class="nt">&lt;/div&gt;</span>

<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;footer&quot;</span><span class="nt">&gt;</span><span class="cp">&lt;?php</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">partial</span><span class="p">(</span><span class="s2">&quot;shared/footer&quot;</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="nt">&lt;/div&gt;</span>
</pre></div>
</div>
<p>方法 partial() 也接受一个只存在于局部范围的变量/参数的数组作为第二个参数:</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="cp">&lt;?php</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">partial</span><span class="p">(</span><span class="s2">&quot;shared/ad_banner&quot;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="nv">$site</span><span class="o">-&gt;</span><span class="na">id</span><span class="p">,</span> <span class="s1">&#39;size&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;big&#39;</span><span class="p">))</span> <span class="cp">?&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="transfer-values-from-the-controller-to-views">
<h2>控制器传值给视图（Transfer values from the controller to views）<a class="headerlink" href="#transfer-values-from-the-controller-to-views" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 可以在每个控制器中使用视图变量 ($this-&gt;view)。 你可以在控制器动作中使用视图对象的setVar()方法直接设置视图变量。</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
    <span class="p">{</span>

    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">showAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//Pass all the posts to the views</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">setVar</span><span class="p">(</span><span class="s2">&quot;posts&quot;</span><span class="p">,</span> <span class="nx">Posts</span><span class="o">::</span><span class="na">find</span><span class="p">());</span>

        <span class="c1">//Using the magic setter</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">posts</span> <span class="o">=</span> <span class="nx">Posts</span><span class="o">::</span><span class="na">find</span><span class="p">();</span>

        <span class="c1">//Passing more than one variable at the same time</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">setVars</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
            <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="nv">$post</span><span class="o">-&gt;</span><span class="na">title</span><span class="p">,</span>
            <span class="s1">&#39;content&#39;</span> <span class="o">=&gt;</span> <span class="nv">$post</span><span class="o">-&gt;</span><span class="na">content</span>
        <span class="p">));</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>名为setvar()的第一参数值的变量将在视图中创建的，并且可以被使用。变量可以是任何类型：从一个简单的字符串，整数等等，变为更复杂的结构，如数组，集合。</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;post&quot;</span><span class="nt">&gt;</span>
<span class="cp">&lt;?php</span>

  <span class="k">foreach</span> <span class="p">(</span><span class="nv">$posts</span> <span class="k">as</span> <span class="nv">$post</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="s2">&quot;&lt;h1&gt;&quot;</span><span class="p">,</span> <span class="nv">$post</span><span class="o">-&gt;</span><span class="na">title</span><span class="p">,</span> <span class="s2">&quot;&lt;/h1&gt;&quot;</span><span class="p">;</span>
  <span class="p">}</span>

<span class="cp">?&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="using-models-in-the-view-layer">
<h2>在视图中使用模型（Using models in the view layer）<a class="headerlink" href="#using-models-in-the-view-layer" title="永久链接至标题">¶</a></h2>
<p>应用模型在视图层也是可用的。<a class="reference internal" href="../api/Phalcon_Loader.html"><em>Phalcon\Loader</em></a> 将在运行时实例化模型:</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;categories&quot;</span><span class="nt">&gt;</span>
<span class="cp">&lt;?php</span>

    <span class="k">foreach</span> <span class="p">(</span><span class="nx">Categories</span><span class="o">::</span><span class="na">find</span><span class="p">(</span><span class="s2">&quot;status = 1&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="nv">$category</span><span class="p">)</span> <span class="p">{</span>
       <span class="k">echo</span> <span class="s2">&quot;&lt;span class=&#39;category&#39;&gt;&quot;</span><span class="p">,</span> <span class="nv">$category</span><span class="o">-&gt;</span><span class="na">name</span><span class="p">,</span> <span class="s2">&quot;&lt;/span&gt;&quot;</span><span class="p">;</span>
    <span class="p">}</span>

<span class="cp">?&gt;</span>
<span class="nt">&lt;/div&gt;</span>
</pre></div>
</div>
<p>尽管你可以执行模型处理操作，如在视图层 insert() 或  update()，但这是不推荐，因为在一个错误或异常发生时，它不可能将执行流程转发给另一个控制器。</p>
</div>
<div class="section" id="caching-view-fragments">
<h2>缓存视图片段（Caching View Fragments）<a class="headerlink" href="#caching-view-fragments" title="永久链接至标题">¶</a></h2>
<p>有时当你开发动态网站和一些区域不会经常更新，请求的输出是完全相同的。 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 提供缓存全部或部分的渲染输出来提高性能。</p>
<p>将 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 配合 <a class="reference internal" href="cache.html"><em>Phalcon\Cache</em></a> 能提供一种更简单的方法缓存输出片段。你可以手动设置缓存处理程序或一个全局处理程序。</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">showAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//Cache the view using the default settings</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">cache</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">showArticleAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// Cache this view for 1 hour</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">cache</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
            <span class="s2">&quot;lifetime&quot;</span> <span class="o">=&gt;</span> <span class="mi">3600</span>
        <span class="p">));</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">resumeAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//Cache this view for 1 day with the key &quot;resume-cache&quot;</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">cache</span><span class="p">(</span>
            <span class="k">array</span><span class="p">(</span>
                <span class="s2">&quot;lifetime&quot;</span> <span class="o">=&gt;</span> <span class="mi">86400</span><span class="p">,</span>
                <span class="s2">&quot;key&quot;</span>      <span class="o">=&gt;</span> <span class="s2">&quot;resume-cache&quot;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">downloadAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">//Passing a custom service</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">cache</span><span class="p">(</span>
            <span class="k">array</span><span class="p">(</span>
                <span class="s2">&quot;service&quot;</span>  <span class="o">=&gt;</span> <span class="s2">&quot;myCache&quot;</span><span class="p">,</span>
                <span class="s2">&quot;lifetime&quot;</span> <span class="o">=&gt;</span> <span class="mi">86400</span><span class="p">,</span>
                <span class="s2">&quot;key&quot;</span>      <span class="o">=&gt;</span> <span class="s2">&quot;resume-cache&quot;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>当我们没有定义缓存的关键组件，这个组件会自动创建一个经过 <a class="reference external" href="http://php.net/manual/en/function.md5.php">md5</a> 的当前渲染的视图名。它是定义每个关键动作的一个良好实践，这样你可以很容易地识别与每个视图关联的缓存。</p>
<p>当视图组件需要缓存的东西时，就会请求缓存服务的服务容器。
这个服务的服务名称约定为&#8221;viewCache&#8221;：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Cache\Frontend\Output</span> <span class="k">as</span> <span class="nx">OutputFrontend</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Cache\Backend\Memcache</span> <span class="k">as</span> <span class="nx">MemcacheBackend</span><span class="p">;</span>

<span class="c1">//Set the views cache service</span>
<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;viewCache&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>

    <span class="c1">//Cache data for one day by default</span>
    <span class="nv">$frontCache</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">OutputFrontend</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
        <span class="s2">&quot;lifetime&quot;</span> <span class="o">=&gt;</span> <span class="mi">86400</span>
    <span class="p">));</span>

    <span class="c1">//Memcached connection settings</span>
    <span class="nv">$cache</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MemcacheBackend</span><span class="p">(</span><span class="nv">$frontCache</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
        <span class="s2">&quot;host&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;localhost&quot;</span><span class="p">,</span>
        <span class="s2">&quot;port&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;11211&quot;</span>
    <span class="p">));</span>

    <span class="k">return</span> <span class="nv">$cache</span><span class="p">;</span>
<span class="p">});</span>
</pre></div>
</div>
<blockquote class="highlights">
<div><dl class="docutils">
<dt>前端 Phalcon\Cache\Frontend\Output 和服务 &#8216;viewCache&#8217; 必须在服务容器（DI）注册为</dt>
<dd>总是开放的（不共享 not shared）</dd>
</dl>
</div></blockquote>
<p>在视图中使用视图缓存也是有用的，以防止控制器执行过程所产生的数据被显示。</p>
<p>为了实现这一点，我们必须确定每个缓存键是独一无二的。 首先，我们验证缓存不存在或是否过期，再去计算/查询并在视图中显示数据:</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">DownloadController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
    <span class="p">{</span>

        <span class="c1">//Check whether the cache with key &quot;downloads&quot; exists or has expired</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">getCache</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">exists</span><span class="p">(</span><span class="s1">&#39;downloads&#39;</span><span class="p">))</span> <span class="p">{</span>

            <span class="c1">//Query the latest downloads</span>
            <span class="nv">$latest</span> <span class="o">=</span> <span class="nx">Downloads</span><span class="o">::</span><span class="na">find</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
                <span class="s1">&#39;order&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;created_at DESC&#39;</span>
            <span class="p">));</span>

            <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">latest</span> <span class="o">=</span> <span class="nv">$latest</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="c1">//Enable the cache with the same key &quot;downloads&quot;</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">cache</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
            <span class="s1">&#39;key&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;downloads&#39;</span>
        <span class="p">));</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p><a class="reference external" href="https://github.com/phalcon/php-site">PHP alternative site</a> 是实现缓存片段的一个例子。</p>
</div>
<div class="section" id="template-engines">
<h2>模版引擎（Template Engines）<a class="headerlink" href="#template-engines" title="永久链接至标题">¶</a></h2>
<p>模板引擎可以帮助设计者不使用复杂的语法创建视图。Phalcon包含一个强大的和快速的模板引擎，它被叫做叫 <a class="reference internal" href="volt.html"><em>Volt</em></a>。</p>
<p>此外, <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 允许你使用其它的模板引擎而不是简单的PHP或者Volt。</p>
<p>使用不同的模板引擎，通常需要使用外部PHP库并且引入复杂的文本解析来为用户生成最终的输出解析。这通常会增加一些你的应用程序的资源耗费。</p>
<p>如果一个外部模板引擎被使用，<a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 提供完全相同的视图渲染等级，仍然可以尝试在这些模板内访问的更多的API。</p>
<p>该组件使用的适配器，这些适配器帮助 Phalcon 与外部模板引擎以一个统一的方式对话，让我们看看如何整合。</p>
<div class="section" id="creating-your-own-template-engine-adapter">
<h3>创建模版引擎（Creating your own Template Engine Adapter）<a class="headerlink" href="#creating-your-own-template-engine-adapter" title="永久链接至标题">¶</a></h3>
<p>有很多模板引擎，你可能想整合或建立一个自己的。开始使用一个外部的模板引擎的第一步是创建一个适配器。</p>
<p>模板引擎的适配器是一个类，作为 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 和模板引擎本身之间的桥梁。
通常它只需要实现两个方法: __construct() and render()。首先接收 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 和应用程序使用的DI容器来创建引擎适配器实例。</p>
<p>方法render()接受一个到视图文件的绝对路径和视图参数，设置使用$this-&gt;view-&gt;setVar()。必要的时候，你可以读入或引入它。</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Engine</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">MyTemplateAdapter</span> <span class="k">extends</span> <span class="nx">Engine</span>
<span class="p">{</span>

    <span class="sd">/**</span>
<span class="sd">     * Adapter constructor</span>
<span class="sd">     *</span>
<span class="sd">     * @param \Phalcon\Mvc\View $view</span>
<span class="sd">     * @param \Phalcon\DI $di</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span><span class="nv">$view</span><span class="p">,</span> <span class="nv">$di</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">//Initialize here the adapter</span>
        <span class="k">parent</span><span class="o">::</span><span class="na">__construct</span><span class="p">(</span><span class="nv">$view</span><span class="p">,</span> <span class="nv">$di</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="sd">/**</span>
<span class="sd">     * Renders a view using the template engine</span>
<span class="sd">     *</span>
<span class="sd">     * @param string $path</span>
<span class="sd">     * @param array $params</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">render</span><span class="p">(</span><span class="nv">$path</span><span class="p">,</span> <span class="nv">$params</span><span class="p">)</span>
    <span class="p">{</span>

        <span class="c1">// Access view</span>
        <span class="nv">$view</span>    <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_view</span><span class="p">;</span>

        <span class="c1">// Access options</span>
        <span class="nv">$options</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">_options</span><span class="p">;</span>

        <span class="c1">//Render the view</span>
        <span class="c1">//...</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="changing-the-template-engine">
<h3>替换模版引擎（Changing the Template Engine）<a class="headerlink" href="#changing-the-template-engine" title="永久链接至标题">¶</a></h3>
<p>你可以想下面一样从控制器更换或者添加更多的模板引擎：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">PostsController</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// Set the engine</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">registerEngines</span><span class="p">(</span>
            <span class="k">array</span><span class="p">(</span>
                <span class="s2">&quot;.my-html&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;MyTemplateAdapter&quot;</span>
            <span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">showAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="c1">// Using more than one template engine</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="o">-&gt;</span><span class="na">registerEngines</span><span class="p">(</span>
            <span class="k">array</span><span class="p">(</span>
                <span class="s2">&quot;.my-html&quot;</span> <span class="o">=&gt;</span> <span class="s1">&#39;MyTemplateAdapter&#39;</span><span class="p">,</span>
                <span class="s2">&quot;.phtml&quot;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Phalcon\Mvc\View\Engine\Php&#39;</span>
            <span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</div>
<p>你可以完全更换模板引擎或同时使用多个模板引擎。方法 Phalcon\Mvc\View::registerEngines() 接受一个包含定义模板引擎数据的数组。每个引擎的键名是一个区别于其他引擎的拓展名。模板文件和特定的引擎关联必须有这些扩展名。</p>
<p>Phalcon\Mvc\View::registerEngines() 会按照相关顺序定义模板引擎执行。如果 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 发现具有相同名称但不同的扩展，它只会使第一个。</p>
<p>如果你想在应用程序的每个请求中注册一个或一组模板引擎。你可以在创建视图时注册服务：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>

<span class="c1">//Setting up the view component</span>
<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>

    <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>

    <span class="c1">//A trailing directory separator is required</span>
    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s1">&#39;../app/views/&#39;</span><span class="p">);</span>

    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">registerEngines</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
        <span class="s2">&quot;.my-html&quot;</span> <span class="o">=&gt;</span> <span class="s1">&#39;MyTemplateAdapter&#39;</span>
    <span class="p">));</span>

    <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>

<span class="p">},</span> <span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
<p>在 <a class="reference external" href="https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mvc/View/Engine">Phalcon Incubator</a> 有一些适配器可用于数个模板引擎</p>
</div>
</div>
<div class="section" id="injecting-services-in-view">
<h2>注入服务到视图（Injecting services in View）<a class="headerlink" href="#injecting-services-in-view" title="永久链接至标题">¶</a></h2>
<p>每个视图执行内部包含一个 <a class="reference internal" href="../api/Phalcon_DI_Injectable.html"><em>Phalcon\DI\Injectable</em></a> 实例, 提供方便地方式访问应用程序的服务容器。</p>
<p>下面的示例演示如何用一个框架约定好的URL服务写一个 jQuery <a class="reference external" href="http://api.jquery.com/jQuery.ajax/">ajax request</a> 。
&#8220;url&#8221; (usually <a class="reference internal" href="url.html"><em>Phalcon\Mvc\Url</em></a>) 服务被注入在视图由相同名称的属性访问：</p>
<div class="highlight-html+php"><div class="highlight"><pre><span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span><span class="nt">&gt;</span>

<span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">({</span>
    <span class="nx">url</span><span class="o">:</span> <span class="s2">&quot;</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">url</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s2">&quot;cities/get&quot;</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="s2">&quot;</span>
<span class="p">})</span>
<span class="p">.</span><span class="nx">done</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">alert</span><span class="p">(</span><span class="s2">&quot;Done!&quot;</span><span class="p">);</span>
<span class="p">});</span>

<span class="nt">&lt;/script&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="stand-alone-component">
<h2>独立的组件（Stand-Alone Component）<a class="headerlink" href="#stand-alone-component" title="永久链接至标题">¶</a></h2>
<p>在Phalcon的所有部件都可以作为胶水（<em>glue</em>） 组件单独使用，因为它们彼此松散耦合:</p>
<div class="section" id="id3">
<h3>分层渲染（Hierarchical Rendering）<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h3>
<p>如下所示，可以单独使用 <a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a>：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>

<span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>

<span class="c1">//A trailing directory separator is required</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s2">&quot;../app/views/&quot;</span><span class="p">);</span>

<span class="c1">// Passing variables to the views, these will be created as local variables</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setVar</span><span class="p">(</span><span class="s2">&quot;someProducts&quot;</span><span class="p">,</span> <span class="nv">$products</span><span class="p">);</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setVar</span><span class="p">(</span><span class="s2">&quot;someFeatureEnabled&quot;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>

<span class="c1">//Start the output buffering</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">start</span><span class="p">();</span>

<span class="c1">//Render all the view hierarchy related to the view products/list.phtml</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s2">&quot;products&quot;</span><span class="p">,</span> <span class="s2">&quot;list&quot;</span><span class="p">);</span>

<span class="c1">//Finish the output buffering</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">finish</span><span class="p">();</span>

<span class="k">echo</span> <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>
</pre></div>
</div>
<p>使用短的语法也可以:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>

<span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>

<span class="k">echo</span> <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">getRender</span><span class="p">(</span><span class="s1">&#39;products&#39;</span><span class="p">,</span> <span class="s1">&#39;list&#39;</span><span class="p">,</span>
    <span class="k">array</span><span class="p">(</span>
        <span class="s2">&quot;someProducts&quot;</span>       <span class="o">=&gt;</span> <span class="nv">$products</span><span class="p">,</span>
        <span class="s2">&quot;someFeatureEnabled&quot;</span> <span class="o">=&gt;</span> <span class="k">true</span>
    <span class="p">),</span>
    <span class="k">function</span><span class="p">(</span><span class="nv">$view</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">//Set any extra options here</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s2">&quot;../app/views/&quot;</span><span class="p">);</span>
        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setRenderLevel</span><span class="p">(</span><span class="nx">View</span><span class="o">::</span><span class="na">LEVEL_LAYOUT</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="id4">
<h3>简单渲染（Simple Rendering）<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h3>
<p>如下所示，以单独使用 <a class="reference internal" href="../api/Phalcon_Mvc_View_Simple.html"><em>Phalcon\Mvc\View\Simple</em></a>：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View\Simple</span> <span class="k">as</span> <span class="nx">SimpleView</span><span class="p">;</span>

<span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SimpleView</span><span class="p">();</span>

<span class="c1">//A trailing directory separator is required</span>
<span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s2">&quot;../app/views/&quot;</span><span class="p">);</span>

<span class="c1">// Render a view and return its contents as a string</span>
<span class="k">echo</span> <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s2">&quot;templates/welcomeMail&quot;</span><span class="p">);</span>

<span class="c1">// Render a view passing parameters</span>
<span class="k">echo</span> <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s2">&quot;templates/welcomeMail&quot;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;email&#39;</span>   <span class="o">=&gt;</span> <span class="nv">$email</span><span class="p">,</span>
    <span class="s1">&#39;content&#39;</span> <span class="o">=&gt;</span> <span class="nv">$content</span>
<span class="p">));</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="view-events">
<h2>视图事件（View Events）<a class="headerlink" href="#view-events" title="永久链接至标题">¶</a></h2>
<p>如果事件管理器（EventsManager）存在，<a class="reference internal" href="../api/Phalcon_Mvc_View.html"><em>Phalcon\Mvc\View</em></a> 和 <a class="reference internal" href="../api/Phalcon_Mvc_View_Simple.html"><em>Phalcon\Mvc\View</em></a> 能够发送事件到 <a class="reference internal" href="events.html"><em>EventsManager</em></a>。事件触发使用的“view”类型。当返回布尔值false，一些事件可以停止运行。以下是被支持的事件：</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="58%" />
<col width="20%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">事件名称</th>
<th class="head">触发点</th>
<th class="head">是否可以停止?</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>beforeRender</td>
<td>渲染过程开始前触发</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td>beforeRenderView</td>
<td>渲染一个现有的视图之前触发</td>
<td>Yes</td>
</tr>
<tr class="row-even"><td>afterRenderView</td>
<td>渲染一个现有的视图之后触发</td>
<td>No</td>
</tr>
<tr class="row-odd"><td>afterRender</td>
<td>渲染过程完成后触发</td>
<td>No</td>
</tr>
<tr class="row-even"><td>notFoundView</td>
<td>视图不存在时触发</td>
<td>No</td>
</tr>
</tbody>
</table>
<p>下面的例子演示了如何将监听器附加到该组件：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\View</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Events\Manager</span> <span class="k">as</span> <span class="nx">EventsManager</span><span class="p">;</span>

<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span> <span class="p">{</span>

    <span class="c1">//Create an events manager</span>
    <span class="nv">$eventsManager</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventsManager</span><span class="p">();</span>

    <span class="c1">//Attach a listener for type &quot;view&quot;</span>
    <span class="nv">$eventsManager</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s2">&quot;view&quot;</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="nv">$event</span><span class="p">,</span> <span class="nv">$view</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">echo</span> <span class="nv">$event</span><span class="o">-&gt;</span><span class="na">getType</span><span class="p">(),</span> <span class="s1">&#39; - &#39;</span><span class="p">,</span> <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">getActiveRenderPath</span><span class="p">(),</span> <span class="nx">PHP_EOL</span><span class="p">;</span>
    <span class="p">});</span>

    <span class="nv">$view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">View</span><span class="p">();</span>
    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setViewsDir</span><span class="p">(</span><span class="s2">&quot;../app/views/&quot;</span><span class="p">);</span>

    <span class="c1">//Bind the eventsManager to the view component</span>
    <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setEventsManager</span><span class="p">(</span><span class="nv">$eventsManager</span><span class="p">);</span>

    <span class="k">return</span> <span class="nv">$view</span><span class="p">;</span>

<span class="p">},</span> <span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
<p>下面的示例演示如何创建一个插件 <a class="reference external" href="http://www.php.net/manual/en/book.tidy.php">Tidy</a> ，清理/修复的渲染过程中产生的HTML：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">class</span> <span class="nc">TidyPlugin</span>
<span class="p">{</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">afterRender</span><span class="p">(</span><span class="nv">$event</span><span class="p">,</span> <span class="nv">$view</span><span class="p">)</span>
    <span class="p">{</span>

        <span class="nv">$tidyConfig</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
            <span class="s1">&#39;clean&#39;</span>          <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
            <span class="s1">&#39;output-xhtml&#39;</span>   <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
            <span class="s1">&#39;show-body-only&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span>
            <span class="s1">&#39;wrap&#39;</span>           <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span>
        <span class="p">);</span>

        <span class="nv">$tidy</span> <span class="o">=</span> <span class="nx">tidy_parse_string</span><span class="p">(</span><span class="nv">$view</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">(),</span> <span class="nv">$tidyConfig</span><span class="p">,</span> <span class="s1">&#39;UTF8&#39;</span><span class="p">);</span>
        <span class="nv">$tidy</span><span class="o">-&gt;</span><span class="na">cleanRepair</span><span class="p">();</span>

        <span class="nv">$view</span><span class="o">-&gt;</span><span class="na">setContent</span><span class="p">((</span><span class="nx">string</span><span class="p">)</span> <span class="nv">$tidy</span><span class="p">);</span>
    <span class="p">}</span>

<span class="p">}</span>

<span class="c1">//Attach the plugin as a listener</span>
<span class="nv">$eventsManager</span><span class="o">-&gt;</span><span class="na">attach</span><span class="p">(</span><span class="s2">&quot;view:afterRender&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="nx">TidyPlugin</span><span class="p">());</span>
</pre></div>
</div>
</div>
</div>


                    </div>
                  </div>
                </div>
            </div>
          </td>
        </tr>
      </table>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="tags.html" title="视图助手（View Helpers）"
             >下一页</a> |</li>
        <li class="right" >
          <a href="odm.html" title="对象文档映射 ODM (Object-Document Mapper)"
             >上一页</a> |</li> 
      </ul>
    </div>

      <div class="prefooter">
  <div class="container">
      <div class="row">
          <div class="col-sm-3 text-right">
              <span>Follow along:</span>
          </div>
          <div class="col-sm-6 text-center">
              <a href="https://twitter.com/phalconphp" alt="Twitter" class="btn-social btn-social-twitter"><i class="icon-twitter"></i></a>
              <a href="https://www.facebook.com/pages/Phalcon-Framework/134230726685897" alt="Facebook" class="btn-social btn-social-facebook"><i class="icon-facebook"></i></a>
              <a href="https://plus.google.com/102376109340560896457" alt="Google+" class="btn-social btn-social-googleplus"><i class="icon-googleplus"></i></a>
              <a href="https://github.com/phalcon/cphalcon" alt="Github" class="btn-social btn-social-github"><i class="icon-github"></i></a>
          </div>
          <div class="col-sm-3">
          </div>
      </div>
  </div>

</div>
<footer class="footer">
  <div class="container">
      <div class="row">
          <div class="col-xs-4 col-sm-3">
              <h4>Download</h4>
              <ul>
                  <li><a href="http://phalconphp.com/download">Installing Phalcon</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://api.phalconphp.com">API</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html">Tutorial</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html#sample-applications">Sample Applications</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-3">
              <h4>Community</h4>
              <ul>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon">GitHub</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon/issues">Issue Tracker</a></li>
                  <li><a href="http://stackoverflow.com/questions/tagged/phalcon">Stack Overflow</a></li>
                  <li><a href="http://phalconphp.com/en/testimonials">Testimonials</a></li>
                  <li><a href="http://builtwith.phalconphp.com/">Built with Phalcon</a></li>
                  <li><a href="http://store.phalconphp.com/">Store</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-2">
              <h4>About</h4>
              <ul>
                  <li><a class="link-black" href="http://blog.phalconphp.com/">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                  <li><a href="http://phalconphp.com/en/team">Team</a></li>
                  <li><a href="http://phalconphp.com/en/roadmap">Roadmap</a></li>
                  <li><a href="http://phalconphp.com/en/donate">Donate</a></li>
                  <li><a href="http://phalconphp.com/en/consulting">Consulting</a></li>
                  <li><a href="http://phalconphp.com/en/hosting">Hosting</a></li>
              </ul>
          </div>
          <div id="license-spaccer" class="visible-xs"></div>
          <div id="license-wrapper" class="col-xs-12 col-sm-4">
              <p class="license">

                  Found a typo or an error? Want to improve this document? The documentation sources are available on <a href="http://github.com/phalcon/docs">Github</a><br>
                  Need support or have questions? Check our <a href="http://forum.phalconphp.com">Forum</a><br>
                  <br>

                  Phalcon Framework is released under the <a href="https://github.com/phalcon/cphalcon/blob/master/docs/LICENSE.md">new BSD license</a>.<br>
                  Except where otherwise noted, content on this site is licensed under the
                    <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License.</a>

                <div class="design">
                  <span>Designed by:</span>

                  <a href="http://www.fog-city.net/" class="fogcity" target="_blank" title="Fog City Software"><span>Fog City Software</span></a>
              </div>
          </div>
      </div>
  </div>
</footer>

    </div>
    <script type="text/javascript">
    $(window).on("load", function(){
      var cx = '009733439235723428699:lh9ltjgvdz8';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    });
    </script>

  </body>
</html>